package cn.com.yusys.yusp.admin.Utils;


import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;

/**
 * 接口请求处理
 *
 * @autho
 * @since
 * @param <T>
 */
public class ResultUtils<T> {
    private static final String SUCCESS_CODE = "0000";
    private static final String SUCCESS_MESSAGE = "success";
    private static final String FAIL_CODE = "9999";

    private Result<T> result;

    public ResultUtils() {
        result = new Result<>();
        result.setMessage(SUCCESS_MESSAGE);
        result.setCode(SUCCESS_CODE);
        result.setTimestamp(DateFormatUtils.format(new Date(), "yyyy-MM-DD HH:mm:ss.SSS"));
    }

    public Result<T> setData(T t) {
        this.result.setData(t);
        return this.result;
    }

    public Result<T> setErrorMsg(String msg, String detail) {
        this.result.setMessage(msg);
        this.result.setCode(FAIL_CODE);
        this.result.setDetail(detail);
        return this.result;
    }

    public Result<T> setErrorMsg(String msg, String code, String detail) {
        this.result.setMessage(msg);
        this.result.setCode(code);
        this.result.setDetail(detail);
        return this.result;
    }

    public static <T> Result<T> success() {
        return new ResultUtils<T>().setData(null);
    }

    public static <T> Result<T> success(T t) {
        return new ResultUtils<T>().setData(t);
    }

    public static <T> Result<T> error(String msg) {
        return new ResultUtils<T>().setErrorMsg(msg, null);
    }

    public static <T> Result<T> error(String msg, String detail) {
        return new ResultUtils<T>().setErrorMsg(msg, detail);
    }

    public static <T> Result<T> error(String code, String msg, String detail) {
        return new ResultUtils<T>().setErrorMsg(code, msg, detail);
    }

}
